package com.weiyuwqj.controller.account;

import com.weiyuwqj.api.model.BusAccount;
import com.weiyuwqj.api.model.BusIncomeStat;
import com.weiyuwqj.api.service.*;
import com.weiyuwqj.api.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

/**
 * Created by zhangyong on 2016/11/24.
 * 个人资产
 */
@Controller
public class AccountController {
    private static Logger log = Logger.getLogger("AccountController");

    @Autowired
    BusAccountService accountService;
    @Autowired
    BasCouponService couponService;
    @Autowired
    BasTicketService ticketService;
    @Autowired
    BusInvestCollectService investCollectService;
    @Autowired
    BusIncomeStatService incomeStatService;
    @Autowired
    BusInvestService investService;

    /**
     * 个人资产接口
     * @return
     */
    @ResponseBody
    @RequestMapping("/user/account")
    public Object account(HttpServletRequest request){
        Integer userId = (Integer) request.getSession().getAttribute("userId");

        Map map=new HashMap();
        BusAccount account=accountService.getBusAccountByUserId(userId);
        BusIncomeStat incomeStat=incomeStatService.getOneByUserId(userId);
        map.put("account",account);
        map.put("incomeStat",incomeStat);
        return map;
    }

    /**
     * 资产详情页面
     * @param request
     * @return
     */
    @RequestMapping("/user/assets")
    public Object assets(HttpServletRequest request){
        Integer userId = (Integer) request.getSession().getAttribute("userId");
        Map couponMap=new HashMap();
        couponMap.put("userId",userId);
        couponMap.put("status",2);/*
        request.getSession().setAttribute("unusedCoupon", couponService.statCouponCount(couponMap));
        request.getSession().setAttribute("unusedTicket", ticketService.statTicketCount(couponMap));
        couponMap.put("status",1);
        request.getSession().setAttribute("usedCoupon", couponService.statCouponCount(couponMap));
        request.getSession().setAttribute("usedTicket", ticketService.statTicketCount(couponMap));
        Map collectMap = new HashMap();
        collectMap.put("userId",userId);
        Date date =DateUtils.addDays(new Date(),-1);
        collectMap.put("beginDate", DateUtils.getDayStartTime(date));
        collectMap.put("endDate",DateUtils.getNextDayStartTime(date));

        Map csbMap=new HashMap();
        Map cdbMap=new HashMap();
        Map xcbMap=new HashMap();
        Map cxbMap=new HashMap();
        List<Map> yesterdayInterest=investCollectService.sumYesterdayInterest(collectMap);
        for(int i=0 ;i<yesterdayInterest.size() ;i++){
            Map map=yesterdayInterest.get(i);
            if(map.get("itemType")!=null) {
                int type  = Integer.valueOf(map.get("itemType").toString());
                if (type==1) {
                    xcbMap.put("yesterdayInterest", map.get("yesterdayInterest"));
                } else if (type==2) {
                    csbMap.put("yesterdayInterest", map.get("yesterdayInterest"));
                } else if (type==5) {
                    cxbMap.put("yesterdayInterest", map.get("yesterdayInterest"));
                }else {
                    cdbMap.put("yesterdayInterest", map.get("yesterdayInterest"));
                }
            }
        }
        List totalInterest=investCollectService.sumTotalInterest(collectMap);
        for(int i=0 ;i<totalInterest.size() ;i++){
            Map map=(Map)totalInterest.get(i);
            if(map.get("itemType")!=null) {
                int type  = Integer.valueOf(map.get("itemType").toString());
                if (type==1) {
                    xcbMap.put("totalInterest", map.get("totalInterest"));
                } else if (type==2) {
                    csbMap.put("totalInterest", map.get("totalInterest"));
                }else if (type==5) {
                    cxbMap.put("totalInterest", map.get("totalInterest"));
                } else {
                    cdbMap.put("totalInterest", map.get("totalInterest"));
                }
            }
        }
        List wait=investCollectService.sumWait(collectMap);
        log.info("wait=================="+wait);
        for(int i=0 ;i<wait.size() ;i++){
            Map map=(Map)wait.get(i);
            log.info("map=================="+map);
            if(map.get("itemType")!=null) {
                int type  = Integer.valueOf(map.get("itemType").toString());
                if(type==1){
                    xcbMap.put("wait",map.get("waitAmount"));
                }else if(type==2){
                    csbMap.put("wait",map.get("waitAmount"));
                }else if(type==5){
                    cxbMap.put("wait",map.get("waitAmount"));
                }else{
                    cdbMap.put("wait",map.get("waitAmount"));
                }
            }
        }
        request.getSession().setAttribute("csbMap", csbMap);
        request.getSession().setAttribute("cdbMap", cdbMap);
        request.getSession().setAttribute("xcbMap", xcbMap);
        request.getSession().setAttribute("cxbMap", cxbMap);
*/
        return "main/user/assets";
    }

    //五月内投资统计
    @ResponseBody
    @RequestMapping(value="/account/monthlyStat")
    public Object monthlyStat(HttpServletRequest request){
        Integer userId = (Integer) request.getSession().getAttribute("userId");
        Map paramMap= new HashMap<>();
        paramMap.put("userId",userId);
        paramMap.put("beginDate", DateUtils.getMonthStartTime(new Date(),-4));
        //List list = investService.monthlyStat(paramMap);
        return null;
    }

    /**
     * 五月内收益统计
     */
    @ResponseBody
    @RequestMapping(value="/account/incomeStat")
    public Object incomeStat(HttpServletRequest request){
        Integer userId = (Integer) request.getSession().getAttribute("userId");
        Map paramMap= new HashMap<>();
        paramMap.put("userId",userId);
        paramMap.put("beginDate", DateUtils.getMonthStartTime(new Date(),-4));
        //List list = incomeService.monthlyStat(paramMap);
        return null;
    }

    /**
     * 拉取
     */
    @ResponseBody
    @RequestMapping(value="/account/checkAccount")
    public Object checkAccount(){
        // accountService.checkAccount();
        return null;
    }


}
